package idpa.paul.hangman.controller;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;
import idpa.paul.hangman.model.hangmanModel;
import idpa.paul.hangman.view.MainActivity;
import idpa.paul.hangman.view.R;

public class guessLetterController {

	
	//variables
	MainActivity g;
	hangmanModel m;
	
	
	
	//constructor
	public guessLetterController(hangmanModel m, MainActivity g){
		
		this.m = m;
		this.g = g;
		
		
	}
	
	
	
	
	//methods
	public void process(){
		
		//get the letter the user entered
		String theguess = g.guessLetter.getEditableText().toString();
		
		//reset guess text field
		g.guessLetter.setText( "");
		
		//check to see if correct guess
		if(theguess.length()>0){
			char first = theguess.charAt(0);
			boolean isCorrect = m.guessLetter(first);
			g.display_guessedLetters.setText(m.getGuessedLetters());
			if (isCorrect)
			{
				Toast.makeText(g, "You guessed correctly.", Toast.LENGTH_SHORT).show();
				for(int i =0; i<g.letters.length;i++){
					
					g.letters[i].setText(m.getLetter(i)+"  ");
					
				}
				//if won condition
				if(m.countDiscovered()==g.letters.length){
					
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(g);
					alertDialogBuilder.setTitle("Victory");
					alertDialogBuilder.setMessage("You Won!");
					alertDialogBuilder.setCancelable(false);
					alertDialogBuilder.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							new NewGameController(m, g).process();
							dialog.cancel();
							
						}
					});
					
					alertDialogBuilder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							g.finish();
							
						}
					});
					
					alertDialogBuilder.create().show();
					
					
					
				}
			}else{
				
				Toast.makeText(g, "Your guess was wrong. You have "+(5-m.getNumberFailedGuesses())+" guesses remaining.", Toast.LENGTH_SHORT).show();
				int numwrongguesses = m.getNumberFailedGuesses();
				switch(numwrongguesses){
				
				case 1:
					g.hangmanImage.setImageResource(R.drawable.board4);
					break;
				case 2:
					g.hangmanImage.setImageResource(R.drawable.board3);
					break;
				case 3:
					g.hangmanImage.setImageResource(R.drawable.board2);
					break;
				case 4:
					g.hangmanImage.setImageResource(R.drawable.board1);
					break;
				case 5:
					g.hangmanImage.setImageResource(R.drawable.board0);
					break;
				
				default:
					
					break;
				
				
				
				
				}
				
				//if lost
				if(m.getNumberFailedGuesses()>=5){
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(g);
					alertDialogBuilder.setTitle("Defeat");
					alertDialogBuilder.setMessage("You Lost!");
					alertDialogBuilder.setCancelable(false);
					alertDialogBuilder.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							new NewGameController(m, g).process();
							dialog.cancel();
							
						}
					});
					
					alertDialogBuilder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							g.finish();
							
						}
					});
					
					alertDialogBuilder.create().show();
				}
			}
			
		}
		
		
		
		
		
	}
	
	
}
